<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/29/029
 * Time: 12:04
 */

namespace App\HttpController;
use App\Lib\Redis\Redis;
use App\Pool\RedisPool;
use EasySwoole\Component\Di;

class Index extends Base
{
    public function index()
    {
//        $this->response()->write('hello world');
        $this->writeJson(200,'success');
        return '/test';
    }

    /**
     * 原生连接
     */
    public function getred()
    {
//        $redis = new \Redis();
//        $redis->connect('127.0.0.1',6379,5);
//        $redis->set('signwa',900);
//        return $this->writeJson(200,$redis->get('signwa'),'ok');
        $result = Redis::getInstance()->get('signwa');
        return $this->writeJson(200,$result,'ok');
//        return $this->response()->write($result);
    }

    public function getRedByDi()
    {
        $result = Di::getInstance()->get('REDIS')->get('signwa');
        return $this->response()->write($result);
    }

    public function red()
    {
//        RedisPool::invoke(function (RedisObject $redis){
//            $redis->set('key','仙士可');
//            $data = $redis->get('key');
//            $this->response()->write($data);
//        });

//        RedisPool::invoke(function(RedisObject $redis){
//           $redis->HSET('easy_swoole:key:test1',random_int(0,999),'abc');
//           $data = $redis->HGETALL('easy_swoole:key:test1');
//           $this->writeJson(200,$data,'success');
//        });
//        return;

        $redis = RedisPool::defer();
        $redis->HSET('easy_swoole:key:test2',random_int(0,999),$this->randomkeys(random_int(50,200)));
        $data = $redis->HGETALL('easy_swoole:key:test2');
        $this->writeJson(200,$data,'success');

    }

    function randomkeys($length)
    {
        $key = '';
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        for($i=0;$i<$length;$i++)
        {
            $key .= $pattern{mt_rand(0,35)};    //生成php随机数
        }
        return $key;
    }
}